# Hyperledger fabric services deployment file.
#
# Example usage: kubectl create -f <this_file>

{% if fabric.consensus_type | default('kafka') =='kafka' %}
{% for zookeeper in zookeeperls %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ zookeeper }}
  name: {{ zookeeper }}
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ zookeeper }}
  ports:
    - { name: "port1", port: 2888 }
    - { name: "port2", port: 3888 }
    - { name: "port3", port: 2181 }
{% endfor %}

{% for kafka in kafkals %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ kafka }}
  name: {{ kafka }}
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ kafka }}
  ports:
    - { name: "port1", port: 9092 }
    - { name: "port2", port: 9093 }
{% endfor %}
{% endif %}

{% if allcas is defined and (allcas | length > 0) %}
{% for ca in allcas %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ ca.name }}
  name: {{ ca.name }}
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ ca.name }}
{% if fabric.k8s.exposeserviceport %}
  type: NodePort
{% endif %}
  ports:
    - name: port1
      port: 7054

{% endfor %}
{% endif %}

{% if allpeers is defined and (allpeers | length > 0) %}
{% for peer in allpeers %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ peer.name }}
  name: {{ peer.name }}
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ peer.name }}
{% if fabric.k8s.exposeserviceport %}
  type: NodePort
{% endif %}
  ports:
  - name: port1
    port: 7051
  - name: port2
    port: 7052
  - name: port3
    port: 7053
{% if fabric.metrics is defined and fabric.metrics %}
  - name: scrapeport
    port: 9443
{% endif %}
{% if peerprofile is defined and peerprofile %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ peer.name }}-profile
  name: {{ peer.name }}-profile
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ peer.name }}
  ports:
  - name: profileport
    port: 6060
{% endif %}
{% endfor %}
{% endif %}

{% if allorderers is defined and (allorderers | length > 0) %}
{% for orderer in allorderers %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ orderer.name }}
  name: {{ orderer.name }}
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ orderer.name }}
{% if fabric.k8s.exposeserviceport %}
  type: NodePort
{% endif %}
  ports:
    - name: port1
      port: 7050
{% if fabric.metrics is defined and fabric.metrics %}
    - name: scrapeport
      port: 8443
{% endif %}
{% if ordererprofile is defined and ordererprofile %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: {{ orderer.name }}-profile
  name: {{ orderer.name }}-profile
  namespace: "{{ namespace }}"
spec:
  selector:
    k8s-app: {{ orderer.name }}
  ports:
    - name: profileport
      port: 6060
{% endif %}
{% endfor %}
{% endif %}

{% if fabric.metrics is defined and fabric.metrics %}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nodemetricds
  namespace: "{{ namespace }}"
spec:
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      name:  nodemetricds
  template:
    metadata:
      labels:
        name: nodemetricds
    spec:
      containers:
      - name: nodemetric
        image: prom/node-exporter:latest
        securityContext:
          privileged: true
      terminationGracePeriodSeconds: 3

{% for anode in k8snodes %}
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: metricep-{{anode|replace('.','-')}}
  name: metricep-{{anode|replace('.','-')}}
  namespace: "{{ namespace }}"
spec:
  selector:
    nodeName: "{{ anode|replace('.','-') }}"
{% if fabric.k8s.exposeserviceport %}
  type: NodePort
{% endif %}
  ports:
    - name: metricport
      port: 9100
{% endfor %}
{% endif %}
